#include "imagebutton.h"

ImageButton::ImageButton(const std::string &imgPath,
                         const std::string &imgOnPath) {
    _imgPath = imgPath;
    _imgOnPath = imgOnPath;
    // create a event box
    _eventBox = gtk_event_box_new();
    // Connect eventbox to some callbacks
    gtk_signal_connect(GTK_OBJECT(_eventBox), "button_press_event",
        G_CALLBACK(ButtonPressCallback), this);
    gtk_signal_connect(GTK_OBJECT(_eventBox), "enter_notify_event",
        G_CALLBACK(EnterNotifyCallback), NULL);
    gtk_signal_connect(GTK_OBJECT(_eventBox), "leave_notify_event",
        G_CALLBACK(LeaveNotifyCallback), NULL);
    // Load image from file
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(imgPath.c_str(), NULL);
    GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf);
    gtk_container_add(GTK_CONTAINER(_eventBox), image);

    GdkBitmap *bitmap;
    gdk_pixbuf_render_pixmap_and_mask(pixbuf, NULL, &bitmap, 255);
    gtk_widget_shape_combine_mask(_eventBox, bitmap, 0, 0);
    gtk_widget_show_all(_eventBox);
}

ImageButton::~ImageButton() {

}

GtkWidget *ImageButton::GetWidget() const {
    return _eventBox;
}

gboolean ImageButton::OnButtonPress(GtkWidget *widget,
                                    GdkEventButton *event) {
    printf ("button is being clicked.\n");
    return TRUE;
}

gboolean ImageButton::EnterNotifyCallback(GtkWidget *widget,
                                          GdkEventCrossing *event,
                                          gpointer data) {
    // Get the widget in the container
    RefreshButton(widget, _imgOnPath);
    return TRUE;
}

gboolean ImageButton::LeaveNotifyCallback(GtkWidget *widget,
                                          GdkEventCrossing *event,
                                          gpointer data) {
    RefreshButton(widget, _imgPath);
    return TRUE;
}

gboolean ImageButton::ButtonPressCallback(GtkWidget *widget,
                                          GdkEventButton *event,
                                          gpointer data) {
    // Get the ImageButton object's pointer from data
    ImageButton *btn = (ImageButton *)data;
    // Call the custom callback function
    return btn->OnButtonPress(widget, event);
}

void ImageButton::RefreshButton(GtkWidget *eventBox,
                                const std::string &imgPath) {
    // Get the widget in the container
    GList *cList = gtk_container_get_children (GTK_CONTAINER(eventBox));
    GtkWidget *prevImage = (GtkWidget *)cList->data;
    // Remove the image contained in the widget
    gtk_container_remove(GTK_CONTAINER(eventBox), prevImage);
    // Load image from file
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(imgPath.c_str(), NULL);
    GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf);
    gtk_container_add(GTK_CONTAINER(eventBox), image);

    GdkBitmap *bitmap;
    gdk_pixbuf_render_pixmap_and_mask(pixbuf, NULL, &bitmap, 255);
    gtk_widget_shape_combine_mask(eventBox, bitmap, 0, 0);
    gtk_widget_show_all(eventBox);
}

std::string ImageButton::_imgPath;
std::string ImageButton::_imgOnPath;
